import { Controller, Get, Post, ApiInfo, JwtUnless } from '@libs/koa-action-decorator/index'
import { getRolePage, addRole, getRoleDetail, updateRole, getRoleList } from '../services/roleService'
import { verifiRole, Roles } from '../models/roleModel'
import { SuccessModel } from '@utils/respond/ResModel'
export default @ApiInfo({ name: '角色管理接口', catalogue: '系统管理' })
@Controller('/api/admin/system/role')
class RoleControllers {
    @Get('/page')
    @ApiInfo('分页获取角色列表')
    async getRolePage(ctx) {
        const page = ctx.getQueryNumber('page', 1);
        const size = ctx.getQueryNumber('size', 20);
        ctx.body = await getRolePage({ page, size });
    }

    @Get('/list')
    @ApiInfo('获取所有角色')
    async getRoleList(ctx) {
        ctx.body = await getRoleList();
    }

    @Post('/add')
    @ApiInfo('添加角色')
    async addRole(ctx) {
        const body = ctx.request.body;
        const { checkSuccess, checkResult } = verifiRole(body);
        ctx.body = checkSuccess && await addRole(body) || checkResult;
    }

    @Post('/update')
    @ApiInfo('编辑角色')
    async updateRole(ctx) {
        const body = ctx.request.body;
        const { checkSuccess, checkResult } = verifiRole(body);
        ctx.body = checkSuccess && await updateRole(body) || checkResult;
    }


    @Get('/detail')
    @ApiInfo('获取角色详情')
    async getRoleDetail(ctx) {
        const roleId = ctx.getQueryString('roleId')
        ctx.body = await getRoleDetail(roleId);
    }

    @Post('/delete')
    @ApiInfo('删除角色')
    async deleteRole(ctx) {
        const body = ctx.request.body;
        const { ids } = body;
        let result = await deleteByIds(Roles, ids)
        ctx.body = new SuccessModel({ message: result });
    }
}